<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
    	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>批量发货</title>
	    <link rel="stylesheet" href="css/AllStyle.css" />
	    <script type="text/javascript" src="js/common.js" ></script>
	    <script type="text/javascript" src="js/data.js" ></script>
	    <script type="text/javascript" src="js/tmpl.js" ></script>
	</head>
	<body>
		<!-- 页头 -->
	    <section>
		    <script type="text/javascript">
		        AllPageHeader()
		    </script>
	    </section>
	    
	    <!-- 页面  -->
	    <section class="AllContent">
	    	<form>
	    		<div class="FormStyle">
	    			<ul>
	    				<li>产品名称</li>
	    				<li>
	    					<select name="pid" id="pid" onchange="getProductLots()"></select>
	    				</li>
	    				<li>产品批次</li>
	    				<li>
	    					<select name="lot_no" id="lot_no"></select>
	    				</li>
	    				<li>发货网点</li>
	    				<li>
	    					<select name="from_branch" id="from_branch"></select>
	    				</li>
	    				<li>收货网点</li>
	    				<li>
	    					<select name="to_branch" id="to_branch"></select>
	    				</li>
	    				<li>初始化</li>
	    				<li>
	    					<select class="form-control" type="text" name="is_del" id="is_del">
                                <option value="否">否</option>
                                <option value="是">是</option>
                            </select>
	    				</li>
	    				<li>开始编码</li>
	    				<li class="relative">
	    					<img class="scanimg" data-val="1" src="images/scan_01.png">
	    					<input type="text" name="startcode" id="startcode" onkeypress="if (event.keyCode == 13) {
                                                           proccessTextArea(this);
                                                           return false;}"/>
	    				</li>
	    				<li>结束编号</li>
	    				<li class="relative">
	    					<img class="scanimg" data-val="2" src="images/scan_01.png">
	    					<input type="text" name="endcode" id="endcode" onkeypress="if (event.keyCode == 13) {
                                                           proccessTextArea(this);
                                                           return false;}"/>
	    				</li>
	    			</ul>
	    			
	    			<button class="NextButton" type="button" onclick="submitlot();">保存</button>
	    			<p class="TheResults" style="margin:0px 5%;padding:0px;padding-bottom:10px;"></p>
	    		</div>
	    	</form>
	    </section>
	    
	    <section>
	    	<div class="MaskLayer">
		    	<div class="MaskContent">
		    		<p><img src="images/loading5.gif"></p>
		    		<p>数据获取中...</p>
		    	</div>
	    	</div>
	    </section>
	    
	    <section class="eject">
	    	<div class="shadeBack"></div>
	    	<div class="shadConten">
	    		<p><input type="text" id="query" onchange="queryBranch()" placeholder="输入查询的网点名称"></p>
    			<section class="sc_block">
    				
    			</section>
	    	</div>
	    </section>
	    
	</body>
	<script type="text/javascript" src="js/jquery.min.js" ></script>
	<script type="text/javascript" src="js/mui.min.js" ></script>
	<script type="text/javascript">
	    mui.init();
	    var _id = "" , _st = true , state = true;
		$('.AllHeader li:eq(1)').text('批量发货');
		
		$(function () {
            //document.addEventListener('deviceready', onDeviceReady, true);
            $("#homepagemessageid").hide();
            $("#homepagemessageid").html("");

            loadBranch();
            loadProduct();
	
			$('#MaskLayer').on('tap',function(){
				$('.MaskLayer').hide();
			})
       });
		
        
        $('.FormStyle').on("click",".android_select",function(){
			_id = $(this).attr("id");
			$('.eject').show(0);
	        
	        //弹出框左右上下居中
	        var win = $(window).height()/2;
	        var sch = $('.shadConten').css('height');
	       	var scTop = win-(parseInt(sch.substring(0,sch.length-2))/2);
	        $('.shadConten').css({top:scTop});
	        var z_sc =  $('.shadConten').css("height").substring(0,sch.length-2);
	        if(z_sc >= win*2){
	        	$('.shadConten').css({height:win*2-20,top:"10px"});
	        	$('#query').focus();
	        }
	        state = false;
		})
		
		//返回或者关闭弹窗
		mui.plusReady(function() {
			mui.back = function() {
				if(state == false){
					$('.eject').hide();
	       	 		state = true;
	       	 	}else{
	       	 		var current = plus.webview.currentWebview();
	       	 		current.close();
	       	 	}
			};
			
			 $('.scanimg').click(function(){
		    	var val = $(this).attr("data-val");
		    	 mui.openWindow({
				    url: 'Scanner.html?etk=101&index=lot&type='+val, 
				    id:'Scanner.html'
				});
		    })
		});
		
		//添加refresh自定义事件监听
		window.addEventListener('scan',function(){
			var id = event.detail.id;
			var te = event.detail.type;
			
			if(te == 1){
				$("#startcode").val(id);
			}else{
				$("#endcode").val(id);
			}
		});
		
		$('.shadeBack').click(function(){
			$('.eject').hide();
	       	state = true;
		});
		
		$('.eject').on("click",".sc_block p",function(){
			_st = false;
			if(_id == "_from_branch"){
				$('#_from_branch').text($(this).text()).attr({value:$(this).attr("value")});
				$('.eject').hide();
	       		state = true;
	       		setCookie("lot.from_bid", $(this).attr("value"));
            	setCookie("lot.from_name", $(this).text());
			}else{
				$('#_to_branch').text($(this).text()).attr({value:$(this).attr("value")});
				$('.eject').hide();
	       		state = true;
			}
			loadBranch();
		})
		
		function queryBranch() {
	        var q = $("#query").val();
	        if (empty(q)) {
	            loadBranch();
	        } else {
	            $.ajax({
	                type: "post",
	                url: getServerUrl() + "/Open/Company/catbranch",
	                dataType: 'json',
	                data: 'access_token=' + getToken()+'&name='+q,
	                success: function (json) {
	                    if(json.result == "1"){
	                        showBranch(json.branchlist);
	                    }else{
	                        showBranch(json.branchlist);
	                    }
	
	                },
	                error: function () {
	                    alertMessage(L('branch_notice_netCheck'));
	                }
	            });
	
	        }
	        var oTxt=document.getElementById("query"); 
	                oTxt.value=""; 
	    }

        function getProductLots(){
            var pid = $('#pid').val();
            var datas = {access_token:getToken(),pid:pid};
            $.ajax({
                url:getServerUrl()+"/Open/Company/getlotlist",
                type:'post',
                dataType:'json',
                data: datas,
                success:function(result){
                    var lotlist =result.lotlist;
                    var html= '';
                    if(!empty(lotlist)){
                        for(var i= 0;i<lotlist.length;i++){
                            html += '<option value="'+lotlist[i].lot_no+'">'+lotlist[i].lot_no+'</option>';
                        }
                    }else{
                        html += '<option value="0">没有产品批次</option>';
                    }
                    $("#lot_no").html(html);
                }
            });
        }
        
        function proccessTextArea(t) {
            var item_no = getItemNo($(t).val());
            $(t).val(item_no);
            focusArea();
        }
        
        function focusArea() {
            if (empty($("#startcode").val())) {
                $("#startcode").focus();
            }else if(empty($("#endcode").val())){
                $("#endcode").focus();
            }
        }
        
        function loadBranch() {
            $.ajax({
                type: "post",
                url: getServerUrl() + "/Open/Company/getbranchlist", //与此php页面沟通
                dataType: 'json', //从php返回的值以 JSON方式 解释
                data: 'access_token='+getToken(), //
                success: function (json) {//如果调用php成功
                    branchlist = json.branchlist;
                    showBranch(branchlist);
                },
                error:function(json){
                    alertMessage(json.errordescription);
                }
            });
        }
        function loadProduct() {
            $.ajax({
                type: "post",
                url: getServerUrl() + "/Open/Company/getproductlist", //与此php页面沟通
                dataType: 'json', //从php返回的值以 JSON方式 解释
                data: 'access_token='+getToken(), //
                success: function (json) {//如果调用php成功
                    productlist = json.productlist;
                    showProduct(productlist);
                },
                error:function(json){
                    alertMessage(json.errordescription);
                }
            });
        }
        function showBranch(clist) {
            var chtml = '';
            if (!empty(clist)) {
                for (var i = 0; i < clist.length; i++) {
                    if (!empty(clist[i])) {
                    	chtml = chtml + "<p value='" + clist[i].bid + "'>"+ clist[i].name + "</p>";
                    	     
                    }
                }
				$('#from_branch').parent().html("<p class='android_select' id='_from_branch' value='" + clist[0].bid + "'>"+ clist[0].name + "</p>");
				$('#to_branch').parent().html("<p class='android_select' id='_to_branch' value='" + clist[0].bid + "'>"+ clist[0].name + "</p>");
				
				var fromBid = getCookie("lot.from_bid");
		        var fromName = getCookie("lot.from_name");
		        if(!empty(fromBid)&&!empty(fromName)){
            		$('#_from_branch').text(fromName).attr("value",fromBid);
            	}
            }
            $(".sc_block").html(chtml);
            if(getUserBid()){
                $("#from_branch").val(getUserBid());
                $("#from_branch").attr('disabled','disabled');
            }
        }
        function showProduct(plist) {
            var phtml = "";
            if (!empty(plist)) {
                for (var i = 0; i < plist.length; i++) {
                    if (!empty(plist[i])) {
                        phtml = phtml + "<option value='" + plist[i].pid + "'>"
                        + plist[i].name + "</option>";
                    }
                }
            }
            $("#pid").html(phtml);
//          getItemTrackingList();
            getProductLots()
        }

        var sh;
        var datalist = new Array();

        function submitlot() {
            var pid = $("#pid").val();
            var lot_no = $("#lot_no").val();
            var is_del = $('#is_del').val();
            var startcode =  $("#startcode").val();
            var endcode =  $("#endcode").val();
            
            var from_branch = $("#_from_branch").attr("value")
            var from_name = $("#_to_branch").text()
            var to_name = $("#_to_branch").text()
            var to_branch = $("#_to_branch").attr("value")
            if (empty(pid)) {
                alertMessage("请选择产品！");
                return;
            }
            if (empty($("#_from_branch").attr("value"))) {
                alertMessage("请选择发货网点！");
                return;
            }
            if (empty($("#_to_branch").attr("value"))) {
                alertMessage("请选择收货网点！");
                return;
            }
            if(empty(startcode)){
                alertMessage('开始编码不能为空');
                return false;
            }
            if(empty(endcode)){
                alertMessage('结束编码不能为空');
                return false;
            }
            $(".MaskLayer").show();
            var datas = {access_token:getToken(),starcode:startcode,endcode:endcode,pid:pid,from_branch:from_branch,to_branch:to_branch,lot_no:lot_no,is_del:is_del};
            $.ajax({
                type: "post",
                url: getServerUrl() + "/Open/Comtag/deliverys",
                dataType: 'json',
                data: datas,
                success: function (json) {
                    if (json.result == "1") {
                    	$('.MaskLayer').hide();
//                  	$('.TheResults').hide().show(300).text("发送「" + to_name+ "」数据成功！");
						alert("发送「" + to_name+ "」数据成功！");
                    } else {
                    	$('.MaskLayer').hide();
//                  	$('.TheResults').hide().show(300).text("上传数据异常:「" + json.error_description + "」");
						alert("上传数据异常:「" + json.error_description + "」");
                    }
					$("#endcode").val('');
					$("#startcode").val('');
                }
            });


        }
        
        $('.MaskLayer').on('click',function(){
        	$(this).hide();
        });
        
//      function TRsetTimeout(){
//			setTimeout(function () { 
//		        $('.TheResults').hide(300);
//		    },3000);
//		}

//      function getItemTrackingList() {
//          var pid = $('#pid').val();
//          $("#datalistid").html("");
//          $.ajax({
//              type: "get",
//              url: getServerUrl() + "/Open/ComSys/prosource?" + 'access_token='+getToken()+'&pid='+pid,
//              dataType: 'json',
//              success: function (jdata) {
//                  html = "";
//                  for (var i = 0; i < jdata.length; i++) {
//                      html = html + '<li class="list-group-item">'
//                      + '<div class="media">'
//                      + '<div class="pull-right text-success m-t-sm">'
//                          // + '<button class="btn btn-warning btn-sm" onclick="deleteItemTracking(' + jdata[i].id + ');"><i class="fa fa-times"></i>删除</button>'
//                      + '</div>'
//                      + '<div class="media-body">'
//                      + '<div><a href="#">' + jdata[i].start_date + '</a></div>'
//                      + '<small class="text-muted">' + jdata[0].fname + ' &nbsp;&nbsp;&nbsp;&nbsp;</small>'
//                      + '<small class="text-muted"><i class="fa fa-forward"></i><i class="fa fa-forward"></i></small>'
//                      + '<small class="text-muted"> &nbsp;&nbsp;&nbsp;&nbsp;' + jdata[i].tname + ' &nbsp;&nbsp;&nbsp;&nbsp;</small>'
//                          // +'<small class="text-muted">'+jdata.data[i].type+'</small>'
//                      + '</div>'
//                      + '</div>'
//                      + '</li>';
//                  }
//                  html = html + '<li class="list-group-item">'
//                  + '<button type="button" class="btn btn-primary" onclick="openUrl(\'addtracking.html\');"><i class="fa fa-plus"></i>添加溯源过程</button>'
//                  + '</li>';
//                  $("#datalistid").html(html);
//              },
//              error: function () {
//                  //jsonp 方式此方法不被触发.原因可能是dataType如果指定为jsonp的话,就已经不是ajax事件了
//                  //请求出错处理
//                  //alert("请求出错(请检查相关度网络状况.)");
//              }
//          });
//      }

        function onDeviceReady() {
            document.addEventListener("backbutton", onBackKeyDown, false);
        }
        function onBackKeyDown() {
            openUrl("index.html");
        }
	</script>
</html>
